<template>

  <el-dialog :title="$t('page-trade-refund.dialog-add.title')" :visible.sync="dialogVisible" width="30%" :close-on-click-modal="false">

    <el-form ref="main-form" :model="form" :rules="rules" label-width="120px">
      <el-form-item :label="$t('page-trade-refund.dialog-add.tradeNo')" size="mini" prop="tradeNo">
        <el-input placeholder="" v-model="form.tradeNo" @keyup.enter.native="submit" :readonly="tradeNo" clearable></el-input>
      </el-form-item>

      <el-form-item :label="$t('page-trade-refund.dialog-add.transAmount')" size="mini" prop="transAmount">
        <el-input placeholder="" type="number" v-model="form.transAmount" @keyup.enter.native="submit" clearable></el-input>
      </el-form-item>

      <el-form-item :label="$t('page-trade-refund.dialog-add.transReason')" size="mini" prop="transReason">
        <el-input placeholder="" type="textarea" v-model="form.transReason" @keyup.enter.native="submit" clearable></el-input>
      </el-form-item>
    </el-form>

    <div slot="footer" class="dialog-footer">
      <!-- <el-popover
        v-if="id != null"
        placement="top"
        width="160"
        v-model="popDeleteVisible">
        <p>确定删除吗？</p>
        <div style="text-align: right; margin: 0">
          <el-button size="mini" type="default" @click="popDeleteVisible = false">{{ $t('page.form.submit') }}</el-button>
          <el-button type="danger" size="mini" @click="submitDel">{{ $t('page.form.confirm') }}</el-button>
        </div>
        <el-button type="danger" slot="reference"  size="small" style="float: left;" :loading="loading">删 除</el-button>
      </el-popover> -->

      <el-button @click="close" size="small">{{ $t('page-trade-refund.dialog-add.cancel') }}</el-button>
      <el-button type="primary" @click="submit" size="small" :loading="loading">{{ $t('page-trade-refund.dialog-add.submit') }}</el-button>
    </div>

  </el-dialog>
</template>

<script>
  
import ApiMain from '@/api/trade.refund';

export default {
  mixins: [
  ],
  props: [
  ],
  data () {
    return {

      dialogVisible: false,
      popDeleteVisible: false,
      loading: false,

      form: {
        tradeNo: '',
        transAmount: '',
        transReason : '',
        remark: ''
      },

      rules: {
        tradeNo: [
          { required: true, message: this.$t('page.form.tips8') , trigger: 'blur' },
        ],
        transAmount: [
          { required: true, message: this.$t('page.form.tips9') , trigger: 'blur' },
          { pattern: /^(([1-9]{1}\d*)|(0{1}))(\.\d{1,2})?$/, message: '最多两位小数', trigger: 'change'}
        ],
        transReason: [
          { required: true, message: this.$t('page.form.tips10') , trigger: 'blur' },
        ],
      },

      tradeNo  : null,
      data: null,

    }
  },
  mounted () {
  },

  methods: {

    show (tradeNo = null) {

      this.tradeNo = tradeNo;
      this.reset();
      this.form.tradeNo = tradeNo;
      this.dialogVisible = true;

    },

    close () {
      this.dialogVisible = false;
      this.$emit('dialog-close');
    },

    reset () {
      this.form = {
        tradeNo: '',
        transAmount: '',
        transReason : '',
        remark: ''
      };
    },

    fetchData () {

      ApiMain.info({id: this.id})
      .then(re => {
        this.data = re;
      })
      .catch(() => {
        this.data = null;
      })
      .finally(() => {
        this.renderData();
      });
    },

    renderData () {
    },

    verifyForm () {
      var bool = true;
      this.$refs['main-form'].validate((valid) => bool = valid);
      return bool;
    },

    async submit () {

      if (! this.verifyForm()) return false;

      this.loading = true;

      try {
        if (this.id) {
          await this.submitSet();
        }
        else {
          await this.submitAdd();
        }
      } finally {
        this.loading = false;
      }
    },

    async submitDel() {

      this.popDeleteVisible = false;
      this.loading = true;

      await ApiMain.del({id: this.id});

      this.loading = false;
      this.close();
      this.$message({message: this.$t('page-form.form.successfullydelete'), type: 'success'});
    },

    async submitSet () {

      this.form.id = this.id;
      await ApiMain.set(this.form);
      this.$message({message: this.$t('page-form.form.modifysuccessfully'), type: 'success'});
      this.close();

    },

    async submitAdd () {

      let form = Object.assign({}, this.form);
      form.transAmount = Number((form.transAmount * 100).toFixed(0))
      await ApiMain.add(form);
      this.$message({message: this.$t('page-form.form.successfullyadded'), type: 'success'});
      this.close();
    },
  }
}
</script>

<style>

</style>
